﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class AB45 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        AB45Data m_Data, m_DataX;
        public DataSet setUI = new DataSet();
        public DataRow dwABPOS;
        string OrderNumber = "";

        public DataSet dsParamaters;
        #endregion
        public AB45()
        {
            InitializeComponent();
            this.Name = "浆纱单";
            this.txtABPAUFKRS.Text = "生产单";//"07"
            this.txtABPAUFKRS.Text = "SB";
            this.txtABPAUFLFD.Text = "002490";
            this.txtABPAUFPOS.Text = "010";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = true;
            dicParamater["DocType"] = "PRODWARP";
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            m_Data = new AB45Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            InitDataBinding();
        }
        private void InitDataBinding()
        {
            this.setUI.Tables.Add("ABPOS");
            this.setUI.Tables.Add("BABST");
            this.setUI.Tables.Add("GAAUF");
            this.setUI.Tables.Add("ABFOLGE");
            orlReadCommand = OracleDB.CreateCommand();

            #region "ABPOS"
            orlReadCommand.CommandText = "SELECT Distinct ABPAUFKRS ,ABPAUFLFD,ABPAUFPOS,"
            + "ABPART,ABPVAR,ABPFARBE,ABPSTARTDAT,ABPENDDAT"
               + "  FROM INTEX.ABPOS WHERE RowNum<1";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "PRODUCTIONORDER";
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion

            #region "BABST"
            orlReadCommand.CommandText = "SELECT  BMBAUMKRS,BMBAUMLFD, BMKETTTYP,BMSOLLLG "
            + " FROM INTEX.BABST WHERE RowNum<1";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "BABST";

                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    this.setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }

            }
            #endregion

            #region "GAAUF"
            orlReadCommand.CommandText = "SELECT DISTINCT '' AS Composition,GAART, GAVAR,GAFARBE,GAKG1,GAKG3,'' as LOLBLP, '' as ZUPARTIE,GASPUL, '' as GPSPPART,'' as LSNAME1"
                + " FROM INTEX.GAAUF WHERE RowNum<1";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "GAAUF";

                foreach (DataTable dtTemp in dsResult.Tables)
                {

                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion

            #region "ABFOLGE"

            orlReadCommand.CommandText = "SELECT ABFAFOLGE,ABFKSTKRS,ABFKST,ABFSPLPOS,ABFAGANG,ABFBZAGANG,ABFMTYP,ABFSOLLSTART,ABFSOLLDAT "
          + " FROM INTEX.ABFOLGE WHERE RowNum<1";

            dsResult = m_Data.Connect2Proxy(orlReadCommand);

            if (dsResult.Tables.Count > 0)
            {
                dsResult.Tables[0].TableName = "ABFOLGE";

                foreach (DataTable dtTemp in dsResult.Tables)
                {

                    setUI = m_Data.ImportDataTable(setUI, dtTemp);
                }
            }
            #endregion

            setUI.AcceptChanges();
            this.ABPOSDataGrid.DataContext = setUI;
        }
        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;
                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABPOS = drv.Row;
                this.txtABPAUFKRS.Text = dwABPOS["ABPAUFKRS"].ToString();
                this.txtABPAUFLFD.Text = dwABPOS["ABPAUFLFD"].ToString();
                this.txtABPAUFPOS.Text = dwABPOS["ABPAUFPOS"].ToString();
                OrderNumber = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString() + dwABPOS["ABPAUFLFD"].ToString();
                string ProductionOrder = dwABPOS["ABPAUFKRS"].ToString() + dwABPOS["ABPAUFLFD"].ToString() + dwABPOS["ABPAUFPOS"].ToString();
                dwABPOS["COMMENTS"] = m_Data.GetCommentsFromTXTIND("ABP", ProductionOrder);
                ReloadBabst(dwABPOS);
                ReloadGaauf(dwABPOS);
                ReloadABFOLGE(dwABPOS);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void ReloadBabst(DataRow dwABPOS)
        {
            DataSet setResult = new DataSet();
            setResult = m_Data.GetBABST(dwABPOS);
            setUI.Tables["BABST"].Clear();
            setUI.Merge(setResult);
            this.listBabst.DataContext = setUI;
        }

        private void ReloadABFOLGE(DataRow dwABPOS)
        {
            DataSet setResult = new DataSet();
            setResult = m_Data.GetABFOLGE(dwABPOS);
            setUI.Tables["ABFOLGE"].Clear();
            setUI.Merge(setResult);
            this.listABFOLGE.DataContext = setUI;
        }
        private void ReloadGaauf(DataRow dwABPOS)
        {
            DataSet setResult = new DataSet();
            setResult = m_Data.GetGAAUF(dwABPOS);
            setUI.Tables["GAAUF"].Clear();
            setUI.Merge(setResult);
            this.listGAAUF.DataContext = setUI;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow dwTmp = dwABPOS.Table.Clone().Rows.Add();
                dwTmp.ItemArray = dwABPOS.ItemArray;
                DataSet setRpt = new DataSet();
                setRpt = m_Data.BuildProducitonOrderData(dwABPOS);
                dwABPOS.ItemArray = dwTmp.ItemArray;
                dicParamater["ABPAUFKRS"] = dwABPOS["ABPAUFKRS"].ToString();
                dicParamater["ABPAUFLFD"] = dwABPOS["ABPAUFLFD"].ToString();
                dicParamater["ABPAUFPOS"] = dwABPOS["ABPAUFPOS"].ToString();
                m_DataX = new AB45Data(dicParamater);
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                AB45Report m_rpt = new AB45Report(dicParamater);
                m_rpt.dsSource = setRpt;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.GetReport("EXCEL");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //dsParamaters = ((IntexReportClient.DataSetIntex)(this.FindResource("dataSetIntex")));

                orlReadCommand = OracleDB.CreateCommand();
                string ABPAUFLFD = m_Data.Right("000000" + txtABPAUFLFD.Text, 6);

                if (ABPAUFLFD == "000000")
                {
                    orlReadCommand.CommandText = "SELECT Distinct ABPAUFKRS ,ABPAUFLFD,ABPAUFPOS,ABPART,ABPVAR," +
                        "ABPFARBE,ABPSTARTDAT,ABPENDDAT,'' as COMMENTS"
                        + " FROM INTEX.ABPOS WHERE  ABPAUFART='VW' AND ABPERLKZ <>'J' ";
                }
                else
                {
                    orlReadCommand.CommandText = "SELECT Distinct ABPAUFKRS ,ABPAUFLFD,ABPAUFPOS,ABPART,ABPVAR," +
                        "ABPFARBE,ABPSTARTDAT,ABPENDDAT,'' as COMMENTS"
                        + " FROM INTEX.ABPOS WHERE  ABPAUFART='VW' AND ABPERLKZ <>'J' AND ABPAUFLFD =: ABPAUFLFD";
                    orlReadCommand.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = ABPAUFLFD;
                }

                dsResult = m_Data.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "PRODUCTIONORDER";
                }
                setUI.Merge(dsResult);
                setUI.AcceptChanges();
                this.DataContext = setUI;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}
